//
// Created by REEJI2219 on 2024-08-17.
//
#include <iostream>
using namespace std;
class StringNode {
public:
    char *string;
    StringNode *next;
    int max_size;
    StringNode() {
        max_size = 3;
        string = new char[max_size];
        next=nullptr;
    }
    ~StringNode() {
        delete[] string;
        while (next) {
            StringNode *nextNode=next->next;
            delete[] next->string;
            next = nextNode;
        }
    }
};
class LinkString {
    StringNode *head;
    StringNode *tail;
    int length;
public:
    LinkString(char *strs) {
        head=new StringNode();
        tail = head;
        head = appendString(0,strs,this->length);
        while (tail->next!=nullptr) {
            tail = tail->next;
        }
        cout<<this->length;
    }
    StringNode * appendString(int start,char *strs,int &end) {
        StringNode *node=new StringNode();
        int i=0;
        char a = strs[start];
        while (a!='\0') {
            node->string[i]=a;
            i++;
            a = strs[start+i];
            end++;
            if(i==node->max_size) {
                node->next =  appendString(start+i,strs,end);
                return node;
            }
        }
        if(i>0) {
            return node;
        }else {
            return nullptr;
        }
    }
    LinkString appendString(LinkString *str) {
        this->tail->next = str->head;
        this->tail = str->tail;
        this->length = this->length+str->length;
    }
    void printString() {
        printString(this->head);
    }
    bool printString(StringNode *node) {
        if(node==nullptr) {
            return false;
        }
        char str = node->string[0];
        int i=0;
        while (str!='\0') {
           cout<<str;
            i++;
            str = node->string[i];
            if(i==node->max_size) {
               return printString(node->next);
            }
        }
        return true;
    }
};

// int main(int argc, char *argv[]) {
//     char *strs = "aaaabbbbccccddddeeeeffffgggghhhh";
//     LinkString *link_string = new LinkString(strs);
//     LinkString *str2 = new LinkString("iiiijjjjkkkk");
//     // link_string->appendString(str2);
//     link_string->printString();
// }
